package com.yxq.mdt.app.modules.personal.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.modules.personal.bean.RechargeListBean;

import java.util.List;

/**
 * @author L
 * @time 2019-11-18
 * @describe 充值额度列表$
 */
public class RechargeItemListAdapter extends BaseQuickAdapter<RechargeListBean.DataBean.ListBean, BaseViewHolder> {
    private int checkedPosition = 0;
    public EditText editText;

    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    public int getCheckedPosition() {
        return checkedPosition;
    }

    public RechargeItemListAdapter(int layoutResId, @Nullable List<RechargeListBean.DataBean.ListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, RechargeListBean.DataBean.ListBean item) {

//        if (getItemCount() - 1 == checkedPosition && getItemCount() - 1 == helper.getAdapterPosition()) {
//            helper.setGone(R.id.item_et_recharge, true);
//            helper.setGone(R.id.item_tv_recharge, false);
//            editText = helper.getView(R.id.item_et_recharge);
//            editText.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//                }
//
//                @Override
//                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//
//                }
//
//                @Override
//                public void afterTextChanged(Editable editable) {
//                    Long number = Long.parseLong(editText.getText().toString());
//                    if (number < 100) {
//                        ToastUtils.showShort("充值数额最低为100");
//                        return;
//                    }
//                    if (number > 1000000) {
//                        ToastUtils.showShort("充值数额最大为100万");
//                        return;
//                    }
//                    if (number % 100 != 0) {
//                        ToastUtils.showShort("充值数额必须为100的倍数");
//                        return;
//                    }
//
//                }
//            });
//            showSoftInput(editText);
//        } else {
//            if (getItemCount() - 1 == helper.getAdapterPosition()) {
//                helper.setText(R.id.item_tv_recharge, "自定义蜜豆数");
//            } else {
        helper.setText(R.id.item_tv_recharge, item.getMoney() + "蜜豆/" + item.getPrice() + "元");
//            }
        helper.setGone(R.id.item_tv_recharge, true);
        helper.setGone(R.id.item_et_recharge, false);
        if (checkedPosition == helper.getAdapterPosition()) {
            helper.setBackgroundRes(R.id.item_tv_recharge, R.drawable.shape_button_confirm_yellow_selector_6);
        } else {
            helper.setBackgroundRes(R.id.item_tv_recharge, R.drawable.shape_button_confirm_white_selector_6);
        }
//        }
    }

    /**
     * 弹出软键盘
     *
     * @param editText
     */
    private void showSoftInput(EditText editText) {
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
        InputMethodManager inputManager = (InputMethodManager) editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

}
